# 12，图像金字塔操作
import cv2 as cv
import numpy as np

# 高斯金字塔
def pyramid_demo(image):
    level = 2  # 设置金字塔的层数为2
    temp = image.copy()  # 拷贝图像
    pyramid_images = []  # 建立一个空列表
    for i in range(level):
        dst = cv.pyrDown(temp)  # 先对图像进行高斯平滑，然后再进行降采样（将图像尺寸行和列方向缩减一半）
        pyramid_images.append(dst)  # 在列表末尾添加新的对象
        cv.imshow("pyramid" + str(i), dst)
        temp = dst.copy()
    return pyramid_images

# 拉普拉斯金字塔
def laplacian_demo(image):
    pyramid_images = pyramid_demo(image)  # 做拉普拉斯金字塔必须用到高斯金字塔的结果
    level = len(pyramid_images)
    print("level", level)
    for i in range(level - 1, -1, -1):
        if (i - 1) < 0:
            expand = cv.pyrUp(pyramid_images[i], dstsize=image.shape[:2])
            lpls = cv.subtract(image, expand)
            cv.imshow("lapalian_down_" + str(i), lpls)
        else:
            expand = cv.pyrUp(pyramid_images[i], dstsize=pyramid_images[i - 1].shape[:2])
            lpls = cv.subtract(pyramid_images[i - 1], expand)
            cv.imshow("lapalian_down_" + str(i), lpls)

if __name__ == '__main__':
    src = cv.imread('leena.jpg')
    print("src:", src.shape)
    # 重新修改一下输入图片的尺寸
    src = cv.resize(src, (512, 512), cv.INTER_CUBIC)  # 注意：拉普拉斯金字塔时，这里原图若不是2^n×2^n会报错。
    cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)  # 设置为WINDOW_AUTOSIZE或者0可以任意缩放
    cv.imshow('input_image', src)
    laplacian_demo(src)
    cv.waitKey(0)
    cv.destroyAllWindows()
